#ifndef __COMMANDTREE_H__
#define __COMMANDTREE_H__



#include"define.h"
#include"utils.h"
#include<ext/hash_map>
#include<iterator>
#include<iomanip>

#define INIT_TREE_NODE(node) do { \
	node->tailFlag = false;  \
	node->command = NULL; \
	node->command_ext = NULL; \
	node->arg = NULL; \
	node->left = node->right = node->front = node->next = NULL; \
}while(0)




//命令树节点
struct commandTree_st{
	bool tailFlag;  //叶子节点标志;
	char * command;
	int (*command_ext)(void *arg); 
	void * arg; //如果命令后面有参数的话 
	struct commandTree_st * left;
	struct commandTree_st * right;
	struct commandTree_st * front;
	struct commandTree_st * next;
};



//命令树的类;
class commandTree{
public:
	commandTree():root(NULL){ Index.clear() ; utils.registerCmdFunctionInit(); }
	~commandTree(){};
	int insertCmdTreeNode(int CmdType,char * cmd);
	int parseCmd(char * cmd, std::vector<char *> &vec);
	int deleteCommandTree();
	int splitCmdIntoTmpVector(char *cmd, std::vector<char *> &vec);
	int searchCmdTreeNode(int CmdType, std::vector<char *> &cmdnode);
	inline struct commandTree_st * getCmdTreeOption(){ return this->root; }
public:
	static void displayCmdTree(struct commandTree_st *node);
private:
	Utils utils;
	int matchCmd(int CmdType,struct commandTree_st *node, std::vector<char *> &vec, unsigned int index);
	int createCmdTreeNode(int CmdType,struct commandTree_st *node, std::vector<char *> &vec, unsigned int index);
	struct commandTree_st  *root;
	__gnu_cxx::hash_map<char *,struct commandTree_st *>  Index; //减少搜索的范围;
};






#endif 
